package com.csx.springcloud.invoke.openfeign.consumer.base.loadbalancer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 注意,由于 LoadBalancerClient 中明确包含了 @Configuration,这里其实没有必要添加 @Configuration 注解,这里加上的目的是为了起到提示作用
 */
@Configuration
@LoadBalancerClient(name = "provider", configuration = ProviderLoadBalancerConfiguration.ProviderContextInnerLoadBalancerConfiguration.class)
public class ProviderLoadBalancerConfiguration {


    @Autowired
    public Environment environment;

    /**
     * 特别注意，该类不应该被 {@link org.springframework.context.annotation.Configuration} 注解标注
     * 否则会被项目父容器扫描到,从而是的该配置在所有的 LoadBalancerClient 中都生效
     * 我们利用 LoadBalancerClient 注解的 name="provider" 和 configuration=该类.class 使得该类只在 provider 对应的子容器中生效
     */
     class ProviderContextInnerLoadBalancerConfiguration {

        /**
         * 通过外部类传入 项目父容器 中的 Environment
         */
        @Bean
        public ReactorServiceInstanceLoadBalancer dynamicLoadBalancer() {
            return new CustomDynamicLoadBalancer(environment);
        }

        @Bean
        public CustomLoadBalancerFactory csxLoadBalancerFactory() {
            return new CustomLoadBalancerFactory();
        }
    }



}
